from google.appengine.ext.db import djangoforms

from blog.decorators import render_to, JsonResponse, ajax_request
from blog.models import BlogPost
from blog.utils import utc_from_localtime, localtime_from_utc

import logging

@render_to('blog/index.html')
def index(request):
    return {'world': 'world'}

class BlogPostForm(djangoforms.ModelForm):
    class Meta:
        model = BlogPost
    def clean_tags(self):
        data = self.cleaned_data['tags']
#        if data.find(','):
#            return map(data.split(','), strip)
        return data.split(' ')

@render_to('blog/blog.html')
def list(request):
    if request.method == 'POST':
        form = BlogPostForm(request.POST)
        if form.is_valid():
            blogpost = form.save(commit=False)
            blogpost.timestamp = utc_from_localtime(blogpost.timestamp)
            blogpost.put()

    form = BlogPostForm()
    blogposts = BlogPost.all().order('-timestamp')
    title = "Brenden Jeon's Blog"
    return {
        'title': title,
        'form': form,
        'blogposts': blogposts,
        }